package zhengdaoyang.settings.web.controller;

import zhengdaoyang.settings.domain.User;
import zhengdaoyang.settings.service.UserService;
import zhengdaoyang.settings.service.impl.UserServiceImpl;
import zhengdaoyang.utils.PrintJson;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class UserController extends HttpServlet {
    private final UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入到用户控制器");

        String path = request.getServletPath();

        System.out.println(path + "  1111111111");
        if ("/settings/user/login".equals(path)) {
            login(request, response);
        } else if ("/settings/user/findid".equals(path)) {
            findid(request, response);
        }else if("/settings/user/updatePassword".equals(path)){
            updatePassword(request,response);
        }else if("/settings/user/registers".equals(path)){
            insertuser(request,response);
        }
    }
    /*注册用户*/
    private void insertuser(HttpServletRequest request, HttpServletResponse response) {
        String loginAct = request.getParameter("loginAct");
        String loginPwd = request.getParameter("loginPwd");

        int kk = 0;
        try{
            kk=   userService.insertuser(loginAct,loginPwd);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (kk!=0){
            PrintJson.printJsonFlag(response, true);
        }
    }

    /*更新用户的密码*/
    private void updatePassword(HttpServletRequest request, HttpServletResponse response) {
        String  username =  request.getParameter("username");
        String  newpwd =  request.getParameter("newpwd");
        int kk = 0;
        try{
            kk=   userService.updatePassword(username,newpwd);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (kk!=0){
            PrintJson.printJsonFlag(response, true);
        }
    }

    //根据用户名查id
    private void findid(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        int kk = 0;
        try{
          kk=   userService.findid(username);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (kk!=0){
            PrintJson.printJsonObj(response, kk);
        }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入验证登录操作");
        String loginAct = request.getParameter("loginAct");
        String loginPwd = request.getParameter("loginPwd");

        System.out.println("loginAct=" + loginAct + "   loginPwd" + loginPwd);
        try {
            User user = userService.login(loginAct, loginPwd);

            System.out.println("返回controller控制------");
            request.getSession().setAttribute("user", user);
            PrintJson.printJsonFlag(response, true);

        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();

            System.out.println("控制层捕捉异常login==" + msg);

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("success", false);
            map.put("msg", msg);

            PrintJson.printJsonObj(response, map);
        }


    }


}
